#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re

from pyquery import PyQuery

from base.gsxt_base_worker import GsxtBaseWorker
from common.global_field import Model

'''
1. 搜索没有结果判断功能添加
'''


class GsxtHeiLongJiangWorker(GsxtBaseWorker):
    def __init__(self, **kwargs):
        GsxtBaseWorker.__init__(self, **kwargs)
        self.invest_pattern = 'seeInvest\(\'(.*?)\',\'.*?\'\)'
        self.invest_search_obj = re.compile(self.invest_pattern)

    # 需要存入无搜索结果
    def get_search_result_html(self, keyword, session):
        param_list = []
        try:
            # url = 'http://{host}/searchList.jspx?top=top&checkNo=&searchType=1&entName={keyword}'.format(
            #     keyword=keyword, host=self.host)
            # session.headers[
            #     'Cookie'] = 'JSESSIONID1=0000PjJXzdHS53VzYYCLPekuZam:-1; BSFIT_EXPIRATION=1483349148313; BSFIT_DEVICEID=Qt780Mr03LK-p_CQ26wm5JDWPZhslns-CpSzASAJLzBoD3W22uo08H2wdpDT3UBTDIH0uMRXcnSpKeua3PW-m4mFMy24pTAcKTybB_WJ1eSHm0JSh9yCA1WsyPk-c_A-Uqt-0lEsFwKMPF16RuTzuigVOpPMaI8K; BSFIT_OkLJUJ=FBD2HqkjdxGQvxQDlahJ-dRV_bAEhVDF; JSESSIONID10181=0000uuh9b9rbl55b9-edWgEGsbp:-1'
            # r = self.task_request(session.get, url, proxy_type=2)
            # if r is None:
            #     return param_list, self.SEARCH_ERROR
            #
            # if r.text.find('验证码不正确') != -1:
            #     self.log.error('cookie 已过时...')
            #     return param_list, self.SEARCH_ERROR
            #
            # content = r.text

            # keyword = '国网'
            url = 'http://{host}/index.jspx'.format(host=self.host)
            content = self.get_captcha_geetest(url, '#searchText', '#click', keyword, '#searchtips', proxy_type=2)
            if content is None:
                return param_list, self.SEARCH_ERROR

            jq = PyQuery(content, parser='html')
            tip = jq.find('#searchtipsu1').find('span').eq(1).text()
            if tip == '0':
                return param_list, self.SEARCH_NOTHING_FIND

            item_list = PyQuery(content, parser='html').find('#gggscpnamebox').items()
            for item in item_list:
                i_d = item.attr("data-label")
                if i_d is not None and i_d != '':
                    param_list.append({'id': i_d})
        except Exception as e:
            self.log.exception(e)
            return param_list, self.SEARCH_ERROR

        return param_list, self.SEARCH_SUCCESS if len(param_list) > 0 else self.SEARCH_ERROR

    @staticmethod
    def __get_company_name(text):
        jq = PyQuery(text, parser='html')
        return jq.find('#zhizhao').find('.xinxi').find('tr').eq(0).find('td').eq(1).find('span').text().strip()

    def get_year_info_list(self, text):
        jq = PyQuery(text, parser='html')
        item_list = jq.find("#yearreportTable").find('tr').items()
        for item in item_list:
            try:
                if item.text().find('年度报告') == -1:
                    continue
                year_info = item.find('td').eq(1)
                if year_info is None or year_info == '':
                    continue

                year_list = re.findall('(\d+)', year_info.text())
                year = str(year_list[0]) if len(year_list) > 0 else None
                if year is None:
                    continue

                href = item.find('a').attr('href')
                if href is None or href == '':
                    continue

                yield year, 'http://{host}{href}'.format(host=self.host, href=href)
            except Exception as e:
                self.log.exception(e)

    # 基本信息
    def get_base_info(self, session, i_d):
        # 获取基础信息
        url = 'http://{host}/business/JCXX.jspx?id={ID}'.format(host=self.host, ID=i_d)
        base_info = self.task_request(session.get, url)
        if base_info is None:
            self.log.error('获取基础信息页面失败...')
            return None, None

        return url, base_info.text

    # 出资信息总页码数
    def get_page_num(self, text, tag):
        try:
            jq = PyQuery(text, parser='html')
            page_text = jq.find(tag).find('ul').find('li').eq(1).text()
            search_list = re.findall('\d+', page_text)
            if len(search_list) > 0:
                return int(search_list[0])
        except Exception as e:
            self.log.exception(e)
            return 0
        return 0

    # 出资信息
    def get_contributive_info(self, session, text, i_d, data):
        # 获得页码信息
        page_num = self.get_page_num(text, '#paging') + 1
        if page_num == 1:
            page_num += 1

        for i in xrange(1, page_num):
            try:
                url = 'http://{host}/business/QueryInvList.jspx?pno={page}&order=0&mainId={i_d}'.format(
                    host=self.host, page=i, i_d=i_d)
                r = self.task_request(session.get, url)
                if r is None:
                    continue
                self.append_model(data, Model.contributive_info, url, r.text)

                # 解析文本
                invest_list = self.invest_search_obj.findall(r.text.encode('utf-8'))
                for invest_id in invest_list:
                    url = 'http://{host}/queryInvDetailAction.jspx?invId={i_d}'.format(
                        host=self.host, i_d=invest_id)
                    r = self.task_request(session.get, url)
                    if r is None:
                        continue
                    self.append_model(data, Model.contributive_info, url, r.text, classify=Model.type_detail)
            except Exception as e:
                self.log.exception(e)

    def get_key_person_info(self, session, i_d, data):
        url = 'http://{host}/business/loadMoreMainStaff.jspx?uuid={i_d}&order=1'.format(
            host=self.host, i_d=i_d)
        r = self.task_request(session.get, url)
        if r is None:
            return

        self.append_model(data, Model.key_person_info, url, r.text)

    def get_change_info(self, session, text, i_d, data):
        # 获得页码信息
        page_num = self.get_page_num(text, '#bgxx') + 1
        if page_num == 1:
            page_num += 1

        for i in xrange(1, page_num):
            try:
                url = 'http://{host}/business/QueryAltList.jspx?pno={page}&order=0&mainId={i_d}'.format(
                    host=self.host, page=i, i_d=i_d)
                r = self.task_request(session.get, url)
                if r is None:
                    continue
                self.append_model(data, Model.change_info, url, r.text)
            except Exception as e:
                self.log.exception(e)

    def get_annual_info(self, session, text, data):

        for year, url in self.get_year_info_list(text):
            r = self.task_request(session.get, url)
            if r is None:
                continue
            self.append_model(data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)

    def get_shareholder_info(self, session, text, i_d, data):
        # 获得页码信息
        page_num = self.get_page_num(text, '#invInfo') + 1
        if page_num == 1:
            page_num += 1

        for i in xrange(1, page_num):
            try:
                url = 'http://{host}/business/invInfoPage.jspx?pno={page}&order=0&mainId={i_d}'.format(
                    host=self.host, page=i, i_d=i_d)
                r = self.task_request(session.get, url)
                if r is None:
                    continue
                self.append_model(data, Model.shareholder_info, url, r.text)
            except Exception as e:
                self.log.exception(e)

    def get_branch_info(self, session, i_d, data):
        url = 'http://{host}//business/loadMoreChildEnt.jspx?uuid={i_d}&order=1'.format(
            host=self.host, i_d=i_d)
        r = self.task_request(session.get, url)
        if r is None:
            return
        self.append_model(data, Model.branch_info, url, r.text)

    def get_detail_html_list(self, seed, session, param_list):
        name_error_count = 0
        # 保存企业名称
        data_list = []
        rank = 0
        for item in param_list:
            try:
                i_d = item.get('id', None)
                # 基本信息
                url, base_text = self.get_base_info(session, i_d)
                if url is None or base_text is None:
                    continue

                if base_text.find('无效用户') != -1:
                    self.log.error('数据获取失败, 无效用户, id = {id} {text}'.format(
                        id=i_d, text=base_text))
                    continue

                # 获得公司名称
                company_name = self.__get_company_name(base_text)
                if company_name is None or company_name == '':
                    self.log.error('公司名称解析失败..id = {id} {text}'.format(
                        id=i_d, text=base_text))
                    name_error_count += 1
                    continue

                # 建立数据模型
                rank += 1
                data = self.get_model(company_name, rank, seed, self.province)

                # 存储数据
                self.append_model(data, Model.base_info, url, base_text)

                # 出资信息
                self.get_contributive_info(session, base_text, i_d, data)

                # 主要人员信息
                self.get_key_person_info(session, i_d, data)

                # 变更信息
                self.get_change_info(session, base_text, i_d, data)

                # 获得年报信息
                self.get_annual_info(session, base_text, data)

                # 股东信息
                self.get_shareholder_info(session, base_text, i_d, data)

                # 分支机构
                self.get_branch_info(session, i_d, data)

                data_list.append(data)
            except Exception as e:
                self.log.exception(e)

        self.target_db.insert_batch_data(self.target_table, data_list)
        return len(data_list) + name_error_count
